<?php
namespace app\Admin\controller;
use app\Admin\model\Link as LinkModel;
use app\admin\controller\Base;

class Link extends Base
{
    public function index()
    {
    	$link=new LinkModel();
        if(request()->isPost()){
            $sorts=input('post.');

            foreach ($sorts as $k => $v) {
                $link->update(['id'=>$k,'sort'=>$v]);
            }
            $this->success('更新排序成功！',url('index'));
            return;
        }
        $linkres=$link->order('sort desc')->paginate(3);
        $this->assign('list',$linkres);
        return view();
    }

    public function add()
    {	
    	if(request()->isPost()){
            $data=input('post.');
            $data['addtime']=time();
            $validate = \think\Loader::validate('link');
            if(!$validate->scene('add')->check($data)){
                $this->error($validate->getError());
            }
            $link=new LinkModel;
            if($link->save($data)){
                $this->success('添加链接成功',url('index'));
            }else{
                $this->error('添加链接失败！');
            }
            return;
        }
        return view();
    }

    public function edit(){
    	 if(request()->isPost()){
            $data=input('post.');
            /*var_dump($data);
            die;*/
            $validate = \think\Loader::validate('link');
            if(!$validate->scene('edit')->check($data)){
                $this->error($validate->getError());
            }
            $link = new LinkModel;
            $save=$link->update($data);
            if($save){
                $this->success('修改链接成功！',url('index'));
            }else{
                $this->error('修改链接失败！');
            }
            return;
        }
        $list=db('link')->where(array('id'=>input('id')))->find();
        $this->assign('list',$list);
        return view();
    }

    public function onstatus()
    {
        $id = input('id');
        $link = db('link')->find($id);
        if ($link['status'] == 1) {
            $data = [
                'id' => $id,
                'status' => 0,
            ];
            db('link')->update($data);
            $this->redirect('index');
        } else {
            $data = [
                'id' => $id,
                'status' => 1,
            ];
            db('link')->update($data);
            $this->redirect('index');
        }
    }

    public function ontype()
    {
        $id = input('id');
        $link = db('link')->find($id);
        if ($link['type'] == 1) {
            $data = [
                'id' => $id,
                'type' => 0,
            ];
            db('link')->update($data);
            $this->redirect('index');
        } else {
            $data = [
                'id' => $id,
                'type' => 1,
            ];
            db('link')->update($data);
            $this->redirect('index');
        }
    }

    public function del(){
    	$id=input('id');
		if(db('link')->delete(input('id'))){
			$this->success('删除链接成功！','index');
		}else{
			$this->error('删除链接失败！');
		}
    	
    }



}
